<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\admin\model;

use app\common\model\Store as StoreModel;
use app\admin\model\store\User;
use app\common\library\wechat\Qrcode;

/**
 * 商家记录表模型
 * Class Store
 * @package app\admin\model
 */
class Store extends StoreModel
{

    /**
     * 获取列表数据
     * @param bool $isRecycle
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(bool $isRecycle = false): \think\Paginator
    {
        /*return $this->alias("a")
            ->field("a.*,b.user_name")
            ->leftJoin('store_user b', 'b.store_id = a.store_id and b.is_super = 1')
            ->where('a.is_recycle', '=', (int)$isRecycle)
            ->where('a.is_delete', '=', 0)
            ->order(['a.sort' => 'asc', 'a.create_time' => 'desc'])
            ->paginate(15);*/
        //生成小程序二维码
        /*$Qrcode = new Qrcode("wx283432508855c170","2a278220db935a5e18e5d9b493a86b44");
        $url = $Qrcode->getQrcode("id=1","pages/index/index",430);*/

        return $this->with(['storeuser','zone'])
            ->where('is_recycle', '=', (int)$isRecycle)
            ->where('is_delete', '=', 0)
            ->order(['store_id' => 'desc', 'create_time' => 'desc'])
            ->paginate(15);
    }

    /**
     * 添加商店
     * @param array $data
     * @return bool|false
     * @throws \think\db\exception\DbException
     */
    public function add( array $data): bool
    {
        [$data['province_id'], $data['city_id'], $data['region_id']] = $data['cascader'];
        //判断登录用户名唯一
        if((new User())->where(['user_name' => $data['user_name'],'is_delete'=>0])->find()){
            $this->error = '商家用户名已存在，请重新添加';
            return false;
        }
        $store_id = $this->insertGetId([
            'province_id' => $data['province_id'],
            'license' => $data['license'],
            'city_id' => $data['city_id'],
            'region_id' => $data['region_id'],
            'detail' => $data['detail'],
            'store_name' => $data['store_name'],
            'zone_id' => $data['zone_id'],
            'create_time' => time()
        ]);
        if($store_id){
            return (new User())->add((int)$store_id,[
                'user_name' => $data['user_name'],
                'password' => $data['password']
            ]);
        }
        return false;
    }

    /**
     * 编辑记录
     * @param array $data
     * @return bool
     */
    public function edit(array $data): bool
    {
        [$data['province_id'], $data['city_id'], $data['region_id']] = $data['cascader'];
        return $this->save($data);
    }

    /**
     * 移入移出回收站
     * @param bool $isRecycle
     * @return bool|false
     */
    public function recycle(bool $isRecycle = true): bool
    {
        return $this->save(['is_recycle' => (int)$isRecycle]);
    }
}
